﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using iss.sg4210.se18.team6s.jass.BL;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.Utilities;

namespace iss.sg4210.se18.team6s.Controllers
{
    [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
    public class AdvertiserAccountController : Controller
    {
        AdvertiserFacade advertiserFacade = null;
        public AdvertiserAccountController()
        {
            advertiserFacade = new AdvertiserFacade();
        }
        //
        // GET: /AdvertiserAccount/

        public ActionResult Index()
        {
            return View(advertiserFacade.FindAllAdvertiserAccounts());
        }

        //
        // GET: /AdvertiserAccount/Details/5

        public ActionResult Details(Guid id)
        {
            return View(advertiserFacade.GetAdvertiserAccount(id));
        }

        //
        // GET: /AdvertiserAccount/Create

        public ActionResult Create()
        {
            if (Roles.GetUsersInRole(ViewConstants.ROLE_ADVERTISER).Length == 0)
                ModelState.AddModelError(string.Empty, "No Users found in Advertiser Role!");

            return View();
        }

        //
        // POST: /AdvertiserAccount/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                AdvertiserAccount advertiserAccount = new AdvertiserAccount();
                UpdateModel(advertiserAccount, collection);
                advertiserAccount.UserId = (Guid)Membership.FindUsersByName(collection["UserName"])[collection["UserName"]].ProviderUserKey;
                advertiserFacade.AddAdvertiserAccount(advertiserAccount);
                return RedirectToAction("Index");
            }
            catch (JASSException ex) { ModelState.AddModelError(string.Empty, ex.Message); return View(); }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /AdvertiserAccount/Edit/5

        public ActionResult Edit(Guid id)
        {
            return View(advertiserFacade.GetAdvertiserAccount(id));
        }

        //
        // POST: /AdvertiserAccount/Edit/5

        [HttpPost]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            AdvertiserAccount advertiserAccount = advertiserFacade.GetAdvertiserAccount(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(advertiserAccount);
                advertiserFacade.UpdateAdvertiserAccount(advertiserAccount);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(advertiserAccount);
            }
            catch (Exception ex)
            {
                return View(advertiserAccount);
            }
        }

        //
        // GET: /AdvertiserAccount/Delete/5

        public ActionResult Delete(Guid id)
        {
            return View(advertiserFacade.GetAdvertiserAccount(id));
        }

        //
        // POST: /AdvertiserAccount/Delete/5

        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                advertiserFacade.DeleteAdvertiserAccount(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(advertiserFacade.GetAdvertiserAccount(id));
            }
            catch (Exception ex)
            {
                return View(advertiserFacade.GetAdvertiserAccount(id));
            }
        }

        public ActionResult UsersList(Guid id)
        {
            if (Guid.Empty.CompareTo(id) == 0)
                ViewData[ViewConstants.VIEW_DATA_USERS_LIST] = new SelectList(Roles.GetUsersInRole(ViewConstants.ROLE_ADVERTISER));
            else
                ViewData[ViewConstants.VIEW_DATA_USERS_LIST] = new SelectList(Roles.GetUsersInRole(ViewConstants.ROLE_ADVERTISER), Membership.GetUser(id));

            return View("UsersDropDownList");
        }
    }

    [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
    public class AdvertiserAccountTopUpLogController : Controller
    {
        AdvertiserFacade advertiserFacade = null;
        public AdvertiserAccountTopUpLogController()
        {
            advertiserFacade = new AdvertiserFacade();
        }
        //
        // GET: /AdvertiserAccountTopUpLog/

        public ActionResult Index()
        {
            return View(advertiserFacade.FindAllAdvertiserAccountTopUpLogs());
        }

        //
        // GET: /AdvertiserAccountTopUpLog/Details/5

        public ActionResult Details(int id)
        {
            return View(advertiserFacade.GetAdvertiserAccountTopUpLog(id));
        }

        //
        // GET: /AdvertiserAccountTopUpLog/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AdvertiserAccountTopUpLog/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                AdvertiserAccountTopUpLog advertiserTopUpLog = new AdvertiserAccountTopUpLog();
                UpdateModel(advertiserTopUpLog, collection);
                advertiserFacade.AddAdvertiserAccountTopUpLog(advertiserTopUpLog);

                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /AdvertiserAccountTopUpLog/Edit/5

        public ActionResult Edit(int id)
        {
            return View(advertiserFacade.GetAdvertiserAccountTopUpLog(id));
        }

        //
        // POST: /AdvertiserAccountTopUpLog/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            AdvertiserAccountTopUpLog advertiserTopUpLog = advertiserFacade.GetAdvertiserAccountTopUpLog(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(advertiserTopUpLog);
                advertiserFacade.UpdateAdvertiserAccountTopUpLog(advertiserTopUpLog);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(advertiserTopUpLog);
            }
            catch (Exception ex)
            {
                return View(advertiserTopUpLog);
            }
        }

        //
        // GET: /AdvertiserAccountTopUpLog/Delete/5

        public ActionResult Delete(int id)
        {
            return View(advertiserFacade.GetAdvertiserAccountTopUpLog(id));
        }

        //
        // POST: /AdvertiserAccountTopUpLog/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                advertiserFacade.DeleteAdvertiserAccountTopUpLog(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(advertiserFacade.GetAdvertiserAccountTopUpLog(id));
            }
            catch (Exception ex)
            {
                return View(advertiserFacade.GetAdvertiserAccountTopUpLog(id));
            }
        }
    }

}
